import 'dart:async';
import 'dart:io';

import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart' as p;

import '../http_manager.dart';

Future<GKBMultipartFile> multipartFileFromPath(
  String filePath, {
  String? filename,
  MediaType? contentType,
  final Map<String, List<String>>? headers,
}) async {
  filename ??= p.basename(filePath);
  final file = File(filePath);
  final length = await file.length();
  final stream = file.openRead();
  return GKBMultipartFile(
    stream,
    length,
    filename: filename,
    contentType: contentType,
    headers: headers,
    filePath: filePath,
  );
}

class GKBMultipartFile extends MultipartFile {
  final String filePath;

  GKBMultipartFile(
    super.stream,
    super.length, {
    required this.filePath,
    String? filename,
    MediaType? contentType,
    Map<String, List<String>>? headers,
  }) : super(
          filename: filename,
          contentType: contentType,
          headers: headers,
        );

  static Future<MultipartFile> fromFile(
    String filePath, {
    String? filename,
    MediaType? contentType,
    final Map<String, List<String>>? headers,
  }) async {
    return multipartFileFromPath(
      filePath,
      filename: filename,
      contentType: contentType,
      headers: headers,
    );
  }
}
